﻿package morn.core.components
{
    import flash.events.*;
    import morn.core.handlers.*;

    public class ProgressBar extends Component
    {
        protected var _bg:Image;
        protected var _bar:Image;
        protected var _skin:String;
        protected var _value:Number = 0.5;
        protected var _label:String;
        protected var _barLabel:Label;
        protected var _changeHandler:Handler;

        public function ProgressBar(param1:String = null)
        {
            this.skin = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new Image();
            this._bg = new Image();
            addChild(_loc_1);
            var _loc_1:* = new Image();
            this._bar = new Image();
            addChild(_loc_1);
            var _loc_1:* = new Label();
            this._barLabel = new Label();
            addChild(_loc_1);
            return;
        }// end function

        override protected function initialize() : void
        {
            this._barLabel.width = 200;
            this._barLabel.height = 18;
            this._barLabel.align = "center";
            this._barLabel.stroke = "0x004080";
            this._barLabel.color = 16777215;
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._skin != param1)
            {
                this._skin = param1;
                this._bg.url = this._skin;
                this._bar.url = this._skin + "$bar";
                _contentWidth = this._bg.width;
                _contentHeight = this._bg.height;
                callLater(this.changeLabelPoint);
                callLater(this.changeValue);
            }
            return;
        }// end function

        protected function changeLabelPoint() : void
        {
            this._barLabel.x = (width - this._barLabel.width) * 0.5;
            this._barLabel.y = (height - this._barLabel.height) * 0.5 - 2;
            return;
        }// end function

        public function get value() : Number
        {
            return this._value;
        }// end function

        public function set value(param1:Number) : void
        {
            if (this._value != param1)
            {
                param1 = param1 > 1 ? (1) : (param1 < 0 ? (0) : (param1));
                this._value = param1;
                sendEvent(Event.CHANGE);
                if (this._changeHandler != null)
                {
                    this._changeHandler.executeWith([param1]);
                }
                callLater(this.changeValue);
            }
            return;
        }// end function

        protected function changeValue() : void
        {
            var _loc_1:Array = null;
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            if (this.sizeGrid)
            {
                _loc_1 = this.sizeGrid.split(",");
                _loc_2 = _loc_1[0];
                _loc_3 = _loc_1[2];
                _loc_4 = width - _loc_2 - _loc_3;
                _loc_5 = _loc_4 * this._value;
                this._bar.width = _loc_2 + _loc_3 + _loc_5;
                this._bar.visible = this._bar.width > _loc_2 + _loc_3;
            }
            else
            {
                this._bar.width = width * this._value;
            }
            return;
        }// end function

        public function get label() : String
        {
            return this._label;
        }// end function

        public function set label(param1:String) : void
        {
            if (this._label != param1)
            {
                this._label = param1;
                this._barLabel.text = this._label;
            }
            return;
        }// end function

        public function get bar() : Image
        {
            return this._bar;
        }// end function

        public function get barLabel() : Label
        {
            return this._barLabel;
        }// end function

        public function get sizeGrid() : String
        {
            return this._bg.sizeGrid;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            var _loc_2:* = param1;
            this._bar.sizeGrid = param1;
            this._bg.sizeGrid = _loc_2;
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            this._bg.width = _width;
            this._barLabel.width = _width;
            callLater(this.changeLabelPoint);
            callLater(this.changeValue);
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            this._bg.height = _height;
            this._bar.height = _height;
            callLater(this.changeLabelPoint);
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is Number || param1 is String)
            {
                this.value = Number(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

    }
}
